//
//  MyTabBar.m
//  Fn
//
//  Created by 王展 on 15/9/27.
//  Copyright (c) 2015年 wangzhan. All rights reserved.
//

#import "MyTabBar.h"
#import "PhotoViewController.h"
#import "MovieRootViewController.h"
#import "MineViewController.h"
#import "MusicRootViewController.h"
#import "RootChannelViewController.h"
#import "OtherViewController.h"
@interface MyTabBar ()

@property(nonatomic,retain)UIButton *lastButton;//记录上一个button
@property(nonatomic,retain)UIImageView *b3;//自定义按钮
@property(nonatomic,retain)NSTimer *timer;//时间计时器

@property(nonatomic,retain)UIView *myView;

@end

@implementation MyTabBar

+(instancetype)shareMyTabBar
{
    static MyTabBar *myT = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        myT = [[MyTabBar alloc]init];
    });
    return myT;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //删除现有的tabBar
    CGRect rect = self.tabBar.frame;
    //移除tabbarcontroller下边的长条
 //   [self.tabBar removeFromSuperview];
//    [self.tabBar setHidden:YES];
    
    //添加自定义tabBar
    self.myView = [[UIView alloc] init];
    self.myView.frame = rect;

    self.myView.backgroundColor = [UIColor colorWithRed:50 / 255.0 green:50 / 255.0 blue:50 / 255.0 alpha:1];
    [self.view addSubview:self.myView];
      [self isHiddenTabBar:NO withViewController:nil];
    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 0.5)];
    aView.backgroundColor = [UIColor colorWithRed:50 / 255.0 green:50 / 255.0 blue:50 / 255.0 alpha:1.0];
    [self.myView addSubview:aView];
    
    NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"night"];
    if ([str isEqualToString:@"yes"]) {
#warning 改的颜色
        self.myView.backgroundColor = [UIColor whiteColor];
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor:) name:@"changeColor" object:nil];
    
    //添加按钮1
    UIButton *b1 = [[UIButton alloc] init];
    b1.frame = CGRectMake(0, 0, self.myView.frame.size.width / 5, self.myView.frame.size.height);
    [b1 setImage:[UIImage imageNamed:@"第一个ICON.png"] forState:0];
    UIImage *image1 = [UIImage imageNamed:@"第一个ICON选中.png"];
    image1 = [image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [b1 setImage:image1 forState:UIControlStateSelected];
    b1.tag = 1001;
    [b1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    b1.selected = YES;
    [self.myView addSubview:b1];
    
    //添加按钮2
    UIButton *b2 = [UIButton buttonWithType:(UIButtonTypeCustom)];
    b2.frame = CGRectMake(self.myView.frame.size.width / 5, 0, self.myView.frame.size.width / 5, self.myView.frame.size.height);
    [b2 setImage:[UIImage imageNamed:@"第二个ICON.png"] forState:0];
    UIImage *image2 = [UIImage imageNamed:@"第二个ICON选中.png"];
    image2 = [image2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [b2 setImage:image2 forState:UIControlStateSelected];
    b2.tag = 1002;
    [b2 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.myView addSubview:b2];
    
    //添加按钮4
    UIButton *b4 = [UIButton buttonWithType:(UIButtonTypeCustom)];
    b4.frame = CGRectMake(self.myView.frame.size.width / 5 * 3, 0, self.myView.frame.size.width / 5, self.myView.frame.size.height);
    [b4 setImage:[UIImage imageNamed:@"第四个ICON.png"]forState:0];
    UIImage *image4 = [UIImage imageNamed:@"第四个ICON选中.png"];
    image4 = [image4 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [b4 setImage:image4 forState:UIControlStateSelected];
    b4.tag = 1004;
    [b4 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.myView addSubview:b4];
    
    //添加按钮5
    UIButton *b5 = [UIButton buttonWithType:(UIButtonTypeCustom)];
    b5.frame = CGRectMake(self.myView.frame.size.width / 5 * 4, 0, self.myView.frame.size.width / 5, self.myView.frame.size.height);
    [b5 setImage:[UIImage imageNamed:@"第五个ICON.png"] forState:0];
    UIImage *image5 = [UIImage imageNamed:@"第五个ICON选中.png"];
    image5 = [image5 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [b5 setImage:image5 forState:UIControlStateSelected];
    b5.tag = 1005;
    [b5 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.myView addSubview:b5];

    //添加自定义按钮3
    self.b3 = [[UIImageView alloc] initWithFrame:CGRectMake(self.myView.frame.size.width / 5 * 2 + 18.5 * XWIDTH, (49 - 38 * XWIDTH) / 2, 38 * XWIDTH, 38 * XWIDTH)];

    
    
    
    UIImage *image3 = [UIImage imageNamed:@"第三个ICON@2x.png"];
    image3 = [image3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.b3.image = image3;
    [self.myView addSubview:self.b3];
    //打开UIImgeView的用户交互
    self.b3.userInteractionEnabled = YES;
    //创建轻拍手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [self.b3 addGestureRecognizer:tap];
    
    //创建间隔动画
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    
    //将上一个按钮赋值为第一个按钮
    self.lastButton = b1;
    
    
  //创建主页
    RootChannelViewController *rootVC = [RootChannelViewController alloc];
    UINavigationController *rootNC = [[UINavigationController alloc] initWithRootViewController:rootVC];
    rootNC.navigationBar.translucent = NO;
    //  rootVC.myAgent = self;
 
//    PhotoViewController *shequVC = [[PhotoViewController alloc] init];
//    UINavigationController *shequNC = [[UINavigationController alloc] initWithRootViewController:shequVC];
//    shequNC.navigationBar.translucent = NO;
    //  shequVC.myAgent = self;
    
    MusicRootViewController *musicVC = [[MusicRootViewController alloc] init];
    UINavigationController *musicNC = [[UINavigationController alloc] initWithRootViewController:musicVC];
    musicNC.navigationBar.translucent = NO;
    // otherVC.myAgent = self;
    
    OtherViewController *otherVC = [[OtherViewController alloc] init];
    UINavigationController *otherNC = [[UINavigationController alloc] initWithRootViewController:otherVC];
    otherNC.navigationBar.translucent = NO;
    
    
    
    
    //创建消息
    MovieRootViewController *xiaoxiVC = [[MovieRootViewController alloc] init];
    //  xiaoxiVC.myAgent = self;
    UINavigationController *xiaoxiNC = [[UINavigationController alloc] initWithRootViewController:xiaoxiVC];
    xiaoxiNC.navigationBar.translucent = NO;
    //创建我的
    MineViewController *myVC = [[MineViewController alloc] init];
    UINavigationController *myNC = [[UINavigationController alloc] initWithRootViewController:myVC];
    myNC.navigationBar.translucent = NO;
    //   myVC.myAgent = self;
       
 
    self.viewControllers = [NSArray arrayWithObjects:rootNC , musicNC , otherNC, xiaoxiNC, myNC, nil];
    self.tabBar.translucent = NO;
    
}

-(void)timerAction
{
    //通过改变view的transform属性值中的Z轴坐标来实现旋转
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    //设置旋转速度
    basicAnimation.speed = 0.5;
    //设置旋转度数
    basicAnimation.toValue = [NSNumber numberWithInt:M_PI ];
    //播放完毕之后是否逆向回到原来位置
    [basicAnimation setAutoreverses:NO];
    //是否叠加（追加）动画效果
    [basicAnimation setCumulative:NO];
    [self.b3.layer addAnimation:basicAnimation forKey:@"basicAnimation"];
}

//1,2,4,5按钮点击事件
-(void)buttonAction:(UIButton *)button
{
    self.lastButton.selected = NO;
    button.selected = YES;
    self.lastButton = button;
    self.selectedIndex = button.tag - 1001;
}
//3按钮点击事件
-(void)tapAction
{
    self.lastButton.selected = NO;
    self.selectedIndex = 2;
}

-(void)changeColor:(NSNotification *)notification
{
#warning 改的颜色
    self.myView.backgroundColor = [UIColor whiteColor];
    NSString *str = [notification object];
    if ([str isEqualToString:@"no"]) {
#warning 原有颜色
        self.myView.backgroundColor = [UIColor colorWithRed:50 / 255.0 green:50 / 255.0 blue:50 / 255.0 alpha:1];
    }
    //保存当前状态值
    [[NSUserDefaults standardUserDefaults] setObject:str forKey:@"night"];
}




- (void)isHiddenTabBar:(BOOL)hidden withViewController:(UIViewController *)viewController
{
    //先删掉自己自定义的
 

       [self.myView removeFromSuperview];
    if (hidden) {
        
        [self.tabBar setHidden:NO];
        [self.myView setFrame:self.tabBar.bounds];
        //关键****如果是隐藏就将自定义tabbar添加到系统的tabbar上
        [self.tabBar addSubview:self.myView];
        if (viewController != nil) {
            viewController.hidesBottomBarWhenPushed = YES;
        }
        
    }else
    {
        [self.tabBar setHidden:YES];
        [self.myView setFrame:self.tabBar.frame];
        
        //关键****如果是显示就将自定义tabbar添加到view上
        [self.view addSubview:self.myView];
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
